package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.MatchBean;
import cst.neu.yuejian_31.bean.MatchCollectBean;
import cst.neu.yuejian_31.bean.MatchStateBean;
import cst.neu.yuejian_31.iface.MatchCollectListener;
import cst.neu.yuejian_31.iface.MatchCollectStateListener;
import cst.neu.yuejian_31.iface.MatchDetailListener;
import cst.neu.yuejian_31.model.MatchCollectMdoel;
import cst.neu.yuejian_31.model.MatchCollectStateModel;
import cst.neu.yuejian_31.model.MatchModel;

public class MatchDetailActivity extends BaseActivity implements View.OnClickListener,MatchCollectStateListener,MatchCollectListener,MatchDetailListener{
    private Context context=null;
    private String matchId,userId;

    private TextView matchName,matchIntro,matchAddress,matchTel,matchTimes,matchTimef;
    private ImageView matchPic,btn_return;
    private int state;
    private int collectState;
    private String s;
    private ImageView favorImageButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_detail);
        initView();

        MatchCollectStateModel model=new MatchCollectStateModel();
        model.getmatchState(userId,matchId,this);
        favorImageButton.setOnClickListener(this);
        btn_return.setOnClickListener(this);
        getActivityId();
        initdata();

    }

    private void initView() {
        Intent intent=getIntent();
        matchId=intent.getStringExtra("match_id");
        userId=getUserId();
        matchName= (TextView) findViewById(R.id.match_name_detail);
        matchIntro= (TextView) findViewById(R.id.match_intro);
        matchAddress=(TextView) findViewById(R.id.match_adress);
        matchPic=(ImageView) findViewById(R.id.imageView_match_detail);
        matchTel=(TextView)findViewById(R.id.match_tel);
        matchTimes=(TextView)findViewById(R.id.match_time_s);
        matchTimef=(TextView)findViewById(R.id.match_time_f);
        favorImageButton=(ImageView)findViewById(R.id.imageView_match_collect);
        btn_return= (ImageView) findViewById(R.id.imageButton_return_activitydetail);
    }

    private void getActivityId() {
        Intent intent=getIntent();
        matchId=intent.getStringExtra("match_id");
        Toast.makeText(MatchDetailActivity.this,matchId, Toast.LENGTH_SHORT).show();

    }

    private void initdata() {
        MatchModel model=new MatchModel();
        model.getmatchdetail(matchId,this);
    }

    @Override
    public void onSuccess(MatchStateBean matchStateBean) {
        state=matchStateBean.getState();
        s=String.valueOf(state);
        Toast.makeText(MatchDetailActivity.this,s, Toast.LENGTH_SHORT).show();
        if(state==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
        }
    }

    @Override
    public void onSuccess(MatchCollectBean matchCollectBean) {
        collectState=matchCollectBean.getSuccess();
        if(collectState==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);
            Toast.makeText(MatchDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
            Toast.makeText(MatchDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_match_collect:
                doFavor();
                break;
            case R.id.imageButton_return_activitydetail:
                finish();
                break;
        }
    }

    private void doFavor() {
        MatchCollectMdoel favorModel=new MatchCollectMdoel();
        favorModel.matchCollect(userId,matchId,this);
    }
    @Override
    public void onResponse(MatchBean matchBean) {
        matchName.setText(matchBean.getMatch_name());
        matchIntro.setText(matchBean.getIntro());
        matchTel.setText(matchBean.getPhone());
        matchAddress.setText(matchBean.getAddress());
        matchTimes.setText(matchBean.getStarttime());
        matchTimef.setText(matchBean.getCreatetime());
        Picasso.with(MatchDetailActivity.this).load(Constant.MATH_URL+matchBean.getMatch_pic()).into(matchPic);

    }

    @Override
    public void onFail(String msg) {

    }
}
